﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThongTinNhanKhau.model.BO
{
    class TamTruBO
    {
        model.DAO.TamTruDAO tamTruDAO = new DAO.TamTruDAO();
        private GUI.TamTru f;
        private GUI.NhanKhau nhanKhau;

        public TamTruBO(GUI.TamTru tamTru, GUI.NhanKhau nhanKhau)
        {
            // TODO: Complete member initialization
            this.f = tamTru;
            this.nhanKhau = nhanKhau;
        }

        internal void genarateMaTamTru()
        {
            //genarate MaTamTru
            StringBuilder maTamTru = new StringBuilder();
            Random random = new Random();

            do
            {
                maTamTru.Clear();
                maTamTru.Append("MTT");
                for (int i = 0; i < 5; i++)
                {
                    maTamTru.Append(random.Next(0, 9));
                }
            } while (tamTruDAO.isValidMaTamTru(maTamTru.ToString()));

            this.f.tbMaTamTru.Text = maTamTru.ToString();
        }

        internal void loadDefault(string maNhanKhau)
        {
            DataTable dt = new DataTable();
            dt = tamTruDAO.loadDefault(maNhanKhau);

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    this.f.tbHoTen.Text = dr["HoTen"].ToString();
                    this.f.tbGioiTinh.Text = dr["GioiTinh"].ToString();
                    this.f.tbCMND.Text = dr["CMND"].ToString();
                }
            }
        }

        internal void loadTable(String maNhanKhau)
        {
            DataTable dt = tamTruDAO.loadTable(maNhanKhau);
            if (dt.Rows.Count != 0)
            {
                this.f.gridControlTamTru.DataSource = dt;
            }
            else
            {
                this.f.gridControlTamTru.DataSource = null;
            }
        }

        internal void btnLuu()
        {
            String maTamTru = this.f.tbMaTamTru.Text;
            String maNhanKhau = this.f.tbMaNhanKhau.Text;
            String ngayTamTru = this.f.dateTimeNgayTamTru.Value.ToString("yyyy-MM-dd");
            String noiDangKiTamTru = this.f.tbNoiDangKiTamTru.Text;
            String tinhTrang;

            if ("Mở".Equals(this.f.cboTinhTrang.SelectedItem.ToString()))
            {
                tinhTrang = "Enable";
            }
            else
            {
                tinhTrang = "Disable";
            }

            //if Update
            if (tamTruDAO.isValidMaTamTru(maTamTru))
            {
                if (tamTruDAO.capNhatTamTru(maTamTru, maNhanKhau, ngayTamTru, noiDangKiTamTru, tinhTrang))
                {
                    System.Windows.Forms.MessageBox.Show("Cập nhật thành công");
                    loadTable(maNhanKhau);
                    nhanKhau.loadTable();
                }
            }
            else //if New
            {
                if (tamTruDAO.themMoiTamTru(maTamTru, maNhanKhau, ngayTamTru, noiDangKiTamTru,tinhTrang))
                {
                    System.Windows.Forms.MessageBox.Show("Thêm mới thành công");
                    loadTable(maNhanKhau);
                    nhanKhau.loadTable();
                }
            }
        }

        internal void deleteRow(string maTamTru, string maNhanKhau)
        {
            if (tamTruDAO.deleteRow(maTamTru, maNhanKhau))
            {
                System.Windows.Forms.MessageBox.Show("Xóa thành công");
                loadTable(maNhanKhau);
                nhanKhau.loadTable();
            }
        }
    }
}
